package com.example.preferencescreenexample;

import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.app.Activity;
import android.app.Fragment;
import android.view.Menu;
import android.view.MenuItem;

public class PreferenceActivity extends Activity {
	
	public static class MyPreferenceFragment extends PreferenceFragment{
		@Override
		public void onCreate(Bundle savedInstanceState){
			super.onCreate(savedInstanceState);
			//Add the preferences from
			//the XML file
			addPreferencesFromResource(R.xml.preferences);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preference);
		
		getActionBar().setDisplayHomeAsUpEnabled(true);
		
		//If savedInstanceState is not null,
		//then onCreate has already been run once,
		//so there is no need to create a new Fragment
		if(savedInstanceState == null){
			Fragment prefFrag = new MyPreferenceFragment();
			getFragmentManager().beginTransaction().replace(R.id.preference_fragment_frame, prefFrag).commit();
		}
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//If the ActionBar Home button is pressed,
		//leave this Activity
		if(item.getItemId() == android.R.id.home){
			finish();
		}
		return super.onOptionsItemSelected(item);
	}
	
}
